
package com.jcbase.controller.sys;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections4.CollectionUtils;

import com.jcbase.core.auth.anno.RequiresPermissions;
import com.jcbase.core.controller.JCBaseController;
import com.jcbase.core.model.Condition;
import com.jcbase.core.model.Operators;
import com.jcbase.core.util.CommonUtils;
import com.jcbase.core.util.IWebUtils;
import com.jcbase.core.view.InvokeResult;
import com.jcbase.model.SysGroup;
import com.jcbase.model.SysUser;
import com.jcbase.model.SysUserGroup;
import com.jcbase.model.SysUserRole;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * 用户组管理
 * 
 * @author Administrator
 *
 */

public class GroupController extends JCBaseController {

    @RequiresPermissions(value = { "/sys/group" })
    public void index() {
	HttpServletRequest request = getRequest();
	SysUser sysUser = IWebUtils.getCurrentSysUser(getRequest());
	List<SysUserRole> sysUserRole = SysUserRole.me
		.find("select * from sys_user_role where user_id = " + sysUser.getId());
	String roleId = "";
	for (SysUserRole sysUserRole2 : sysUserRole) {
	    roleId = roleId + sysUserRole2.getRoleId();
	}
	InvokeResult result = SysGroup.me.getZtreeViewList(request);
	this.setAttr("jsonTree", result);
	this.setAttr("roleId", roleId);
	render("group_index.jsp");

    }

    public void refreshTree() {
	HttpServletRequest request = getRequest();
	InvokeResult result = SysGroup.me.getZtreeViewList(request);
	this.setAttr("jsonTree", result);
	this.renderJson(result);
    }

    public void getAllTree() {
	HttpServletRequest request = getRequest();
	InvokeResult result = SysGroup.me.getZtreeViewList(request);
	this.setAttr("jsonTree", result);
	this.renderJson(result);
    }

    @RequiresPermissions(value = { "/sys/group" })
    public void add() {
	Integer pid = this.getParaToInt("pid");
	Integer gid = this.getParaToInt("gid");
	if (gid != null) {
	    this.setAttr("item", SysGroup.me.findById(gid));
	}
	this.setAttr("gid", gid);
	this.setAttr("pid", pid);
	render("group_add.jsp");
    }

    @RequiresPermissions(value = { "/sys/group" })
    public void getListData() {
	String name = this.getPara("name");
	Set<Condition> conditions = new HashSet<Condition>();
	if (CommonUtils.isNotEmpty(name)) {
	    conditions.add(new Condition("name", Operators.LIKE, name));
	}
	Page<SysGroup> pageInfo = SysGroup.me.getPage(this.getPage(), this.getRows(), conditions, this.getOrderby());
	this.renderJson(SysGroup.me.toJqGridView(pageInfo));
    }

    /**
     * 获取改组下所有用户
     */
    @RequiresPermissions(value = { "/sys/group" })
    public void getGroupListData() {
	String id = this.getPara("id");
	SysUser user = IWebUtils.getCurrentSysUser(getRequest());
	List<SysGroup> groups = SysGroup.me.find("select * from sys_group where pid = " + id);
	String str = "(" + id + ",";
	for (SysGroup sysGroup : groups) {
	    str = str + sysGroup.getId() + ",";
	}
	str = str.substring(0, str.length() - 1) + ")";
	// 查询这些组里面用户Id
	String sql = "(select user_id from sys_user_group where group_id in " + str + "and create_user_id = "
		+ user.getId() + ")";

	String select = "select su.*, (select group_concat(name) as roleNames from sys_role where id in(select role_id from sys_user_role where user_id=su.id)) as roleNames";
	StringBuffer sqlExceptSelect = new StringBuffer("from sys_user su where su.id in " + sql);

	Page<SysUser> pageInfo = SysUser.me.getPage(this.getPage(), this.getRows(), select, sqlExceptSelect.toString(),
		this.getOrderby());
	this.renderJson(SysUser.me.toJqGridView(pageInfo));
    }

    @RequiresPermissions(value = { "/sys/group" })
    public void delete() {
	String id = this.getPara("id");
	String type = this.getPara("type");
	if ("group".equals(type)) {
	    // 查找该组下面是否还有子节点
	    List<SysUserGroup> list = SysUserGroup.me.find("select * from sys_user_group where group_id = " + id);
	    if (CollectionUtils.isEmpty(list)) {
		SysGroup.me.deleteById(id);
		this.renderJson(InvokeResult.success());
	    } else {
		this.renderJson(InvokeResult.failure("节点下还有用户，不可删除"));
	    }
	}
	// if ("user".equals(type)) {
	// Db.update("update sys_user set group_id = null where id = " + id);
	// this.renderJson(InvokeResult.success());
	// }
    }

    @RequiresPermissions(value = { "/sys/group" })
    public void deleteGroup() {
	String users = this.getPara("users");
	InvokeResult result = SysGroup.me.deleteUserGroup(users);
	this.renderJson(result);
    }

    @RequiresPermissions(value = { "/sys/group" })
    public void deleteGroupUser() {
	String users = this.getPara("users");
	InvokeResult result = SysUserGroup.me.deleteUserGroup(users);
	this.renderJson(result);
    }

    @RequiresPermissions(value = { "/sys/group" })
    public void getZtree() {

	Integer gid = this.getParaToInt("gid");
	SysUser user = IWebUtils.getCurrentSysUser(getRequest());
	InvokeResult result = SysUser.me.getZtreeViewList(user);
	this.setAttr("jsonTree", result);
	this.setAttr("groupId", gid);
	render("group_user_setting.jsp");
    }

    /**
     * 用户绑定组
     */
    @RequiresPermissions(value = { "/sys/group" })
    @Before(Tx.class)
    public void saveUserGroup() {
	Integer groupId = this.getParaToInt("groupId");
	String userIds = this.getPara("userIds");
	SysUser user = IWebUtils.getCurrentSysUser(getRequest());
	InvokeResult result = SysUserGroup.me.updateUserGroup(userIds, groupId, user.getId());
	// InvokeResult result = SysGroup.me.updateUserGroup(userIds, groupId);
	this.renderJson(result);
    }

    @RequiresPermissions(value = { "/sys/group" })
    public void save() {
	Integer pid = this.getParaToInt("pid");
	Integer gid = this.getParaToInt("gid");
	SysUser user = IWebUtils.getCurrentSysUser(getRequest());
	InvokeResult result;
	// 添加分组
	if (gid == null) {
	    if (pid == null) {
		pid = 0;
	    }
	    if (pid != null && pid < 0) {
		pid = 0;
	    }
	    result = SysGroup.me.addSave(pid, this.getPara("name"), user.getId());
	} else {
	    result = SysGroup.me.updateSave(gid, this.getPara("name"));
	}
	this.renderJson(result);
    }

}
